from django.contrib.auth.models import User
from django.db import models

# Create your models here.
from django.utils import timezone

from article.models import Article


class Comment(models.Model):
    # 作者
    author = models.ForeignKey(
        User,
        on_delete=models.CASCADE,
        related_name='comments'
    )

    # 文章
    article = models.ForeignKey(
        Article,
        on_delete=models.CASCADE,
        related_name='comments'
    )

    # 内容
    content = models.TextField()
    # 创建时间
    create_datetime = models.DateTimeField(verbose_name="创建时间", default=timezone.now)

    # 自关联，实现二级评论 comment.children
    parent = models.ForeignKey(
        'self',
        null=True,
        blank=True,
        on_delete=models.SET_NULL,
        related_name='children'
    )

    def __str__(self):
        return self.content[:20]

    class Meta:
        ordering = ['-create_datetime']

